package com.igame.maingame;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.jme.input.ChaseCamera;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Sphere;

public class ControlBall extends Sphere{
	int mark = 0;
	int lastchange = 0;
    public ControlBall(String name, Vector3f center, int zSamples,
            int radialSamples, float radius) {
        super(name, center, zSamples, radialSamples, radius, false);
    }
    public ControlBall(String name, int zSamples, int radialSamples, float radius) {
        this(name, new Vector3f(0, 0, 0), zSamples, radialSamples, radius);
    }
    public void addMark(int dm){
    	mark += dm;
    	lastchange = dm;
    	invokeCallback(EVENT_TYPE.MARK_CHANGED);
    }
    public int getMark(){
    	return mark;
    }
    public int getMarkRecentChange(){
    	return lastchange;
    }
    public void setMark(int nm){
    	mark = nm;
    	invokeCallback(EVENT_TYPE.MARK_CHANGED);
    }
    public static String getControlBallTag(){
    	return "ControlBall";
    }
    public static ControlBall getControlBall( Node node ){
    	if (((OBJECT_TYPE)node.getUserData(OBJECT_TYPE.class.getName())) == OBJECT_TYPE.CONTROL_BALL)
    		return (ControlBall)node.getUserData(getControlBallTag());
    	else return null;
    }
    
    //callback system
    boolean callbackEnable = true;
    Map<EVENT_TYPE, ControlBallEventCallback> callbackList = new TreeMap<EVENT_TYPE, ControlBallEventCallback>();
    public enum EVENT_TYPE{
    	MARK_CHANGED
    }
    public void addCallback(EVENT_TYPE type,ControlBallEventCallback cb){
    	callbackList.put(type, cb);
    }
    public void removeCallback(EVENT_TYPE type){
    	callbackList.remove(type);
    }
    private void invokeCallback(EVENT_TYPE type){
    	if( callbackList.containsKey(type) && callbackEnable)
    		callbackList.get(type).performAction(this);
    }
    public void setCallbackEnable(boolean enable){
    	callbackEnable = enable;
    }
    public boolean getCallbackEnable(){
    	return callbackEnable;
    }
    
    public interface ControlBallEventCallback{
    	public void performAction(ControlBall source);
    }
    
    Vector3f faceto = new Vector3f(0,0,1);
    Vector3f upvec = new Vector3f(0,1,0);
    float turn_angle = 0;
    public Vector3f getFaceDirection(){
    	float[] angles = {0,turn_angle,0};
    	Quaternion quat = new Quaternion(angles);
    	return quat.mult(faceto);
    }
    public void setFaceAngle( float angle ){
    	turn_angle = angle;
    }
    public void addFaceAngle( float delta ){
    	turn_angle += delta;
    }
    public float getFaceAngle(){
    	return turn_angle;
    }
    
//    public class ControlBallChaseCamera extends ChaseCamera{
//    	public ControlBallChaseCamera(Camera cam, Spatial target, Map<String, Object> props) {
//    		super(cam,target,props);
//    	}
//    }
}
